package push

// package main

import (
	"crypto/tls"
	"fmt"

	"doubimeizhi.com/utility"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
	log "github.com/cihub/seelog"
)

var (
	pushhost   string = "push.app.qkier.com"
	pushAppkey string = "067d2589ad6141799d935fb56495010c"
	pushSecret string = "7fb5e9cfa0184773be03006fc6064645"
)

type PushMsgResp struct {
	Code int
	Msg  string
}

func PushMsg(displayType, title, text, router, expireTime string, userid []string) *PushMsgResp {
	nusb := utility.NewURLSignBuilder(pushAppkey, pushSecret, fmt.Sprintf("https://%s/push/back/push/listcast", pushhost))
	reqURL := nusb.BuildURL()
	// reqURL := fmt.Sprintf("https://%s/push/back/push/listcast?appkey=40b3409b84ce4177b30056605e36e785", activityhost)
	b := httplib.Post(reqURL)
	b.SetHost(pushhost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})

	body := make(map[string]interface{})
	body["display_type"] = displayType
	body["userid"] = userid
	body["appid"] = 1
	body["title"] = title
	body["text"] = text
	custom := make(map[string]interface{})
	custom["router"] = router
	body["custom"] = custom
	body["expire_time"] = expireTime
	body["login_status"] = 1
	b.JSONBody(body)

	resp := &PushMsgResp{}
	if err := b.ToJSON(resp); err != nil {
		log.Errorf("push请求失败，err is : %s", err.Error())
		return nil
	}
	return resp
}

func init() {
	pushhost = beego.AppConfig.DefaultString("backsvr::push_host", "push.app.qkier.com")
	pushAppkey = beego.AppConfig.DefaultString("backsvr::push_appkey", "40b3409b84ce4177b30056605e36e785")
	pushSecret = beego.AppConfig.DefaultString("backsvr::push_secret", "9dd784c63eb14418832b024a053be883")
}

// func main() {

// 	info := GetServiceItem("0316309723961")
// 	fmt.Println(info)
// }
